﻿using CPPEI.Coolzon.Common;
using CPPEI.Coolzon.Data;
using CPPEI.Coolzon.Metadata;
using CPPEI.Coolzon.Server.CacheData;
using CPPEI.Coolzon.Service.Contract;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CPPEI.Coolzon.Server.ServiceImplement
{
    /// <summary>
    /// 根据方案名称获取域列表，如果没有方案名称则获取全部域列表
    /// </summary>
    public class ReqGetDomainListHandler : BaseHandler<ReqGetDomainListData, RespGetDomainListData>
    {
        public ReqGetDomainListHandler()
            : base(Service.Contract.REQUEST_SCENARIO.CONNENT, Service.Contract.REQUEST_COMMAND.CONNECT.GETDOMAINLIST)
        {
        }

        protected override RespGetDomainListData OnHandle(Data.MainServiceData.Request request, ReqGetDomainListData requestData)
        {
            if (!string.IsNullOrEmpty(requestData.SolutionName))
            {
                List<DomainItem> itemList = new List<DomainItem>();
                CacheData.ControlDomain[] domainArray = ServerData.Instance.GetDomainList();
                foreach (var domain in domainArray)
                {
                    if (domain != null && domain.SolutionName == requestData.SolutionName)
                    {
                        DomainItem item = new DomainItem();
                        item.Caption = domain.Caption;
                        item.Id = domain.Id;
                        item.CreatorName = domain.CreatorName;
                        item.CreatorAccount = domain.CreatorAccount;
                        item.SolutionName = domain.SolutionName;
                        item.SolutionCaption = domain.SolutionCaption;
                        itemList.Add(item);
                    }
                }
                RespGetDomainListData responseData = new RespGetDomainListData();
                responseData.Domains = itemList.ToArray();
                return responseData;
            }
            else
            {
                // 获取全部域
                List<DomainItem> itemList = new List<DomainItem>();
                CacheData.ControlDomain[] domainArray = ServerData.Instance.GetDomainList();
                for(int index = 0; index < domainArray.Length; index++)
                {
                    CacheData.ControlDomain domain = domainArray[index];
                    if (domain != null)
                    {
                        DomainItem item = new DomainItem();
                        item.Caption = domain.Caption;
                        item.Id = domain.Id;
                        item.CreatorName = domain.CreatorName;
                        item.CreatorAccount = domain.CreatorAccount;
                        item.SolutionName = domain.SolutionName;
                        item.SolutionCaption = domain.SolutionCaption;
                        itemList.Add(item);
                    }
                }
                RespGetDomainListData responseData = new RespGetDomainListData();
                responseData.Domains = itemList.ToArray();
                return responseData;
            }
        }
    }
}
